// pages/index/index.js
import request from '../../util/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannerList:[],//轮播图数据
    recommendList:[],//推荐歌单数据 
    topList:[],//排行榜数据
    musiclistid:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    
    let bannerListData = await request('/banner', {type: 2});
    this.setData({
      bannerList:bannerListData.banners
    })

    // 获取推荐歌单数据
    let recommendListData = await request('/personalized', {limit: 7});
    this.setData({
     recommendList:recommendListData.result 
    })
    let toplist = await request('/toplist')
    toplist.list.splice(5)
    toplist = toplist.list
    // 获取排行榜数据
    let resultArr = [];
    let index = 0;
    while (index<5) {
      let topListData = await request('/playlist/detail', { id: toplist[index++ ].id })
      // console.log(toplist[index].id)
      let topListItem = {
        name:topListData.playlist.name,
        tracks:topListData.playlist.tracks.slice(0, 3)
      }
      resultArr.push(topListItem)
      this.setData({
        topList: resultArr
      })
      
    } 
  },
    // 跳转推荐歌单页面
  getMoreRecommendMusicList() {
    wx.navigateTo({
      url: '/pages/recommendMusicList/recommendMusicList'
    })
  },
  toRecommendSong(){
    wx.navigateTo({
      url: '../recommendSong/recommendSong',
    })
  },
  toSongDetail(e){
    console.log(e.currentTarget.dataset)
    wx.navigateTo({
      url: '../songDetail/songDetail?song=' + JSON.stringify(e.currentTarget.dataset.song),
    })
  },
  toMusicListSquare(){
    wx.navigateTo({
      url: '../musicListSquare/musicListSquare',
    })
  },
  goToRanking(){
    wx.navigateTo({
      url: '../ranking/ranking',
    })
  },
  toMusicList(e) {
    wx.navigateTo({
      url: '/pages/musicList/musicList?musiclistid=' + e.currentTarget.dataset.musiclistid
    })
    // console.log(e.currentTarget.dataset);
  },
  // 跳转到search页面
  toSearch(){
    wx.navigateTo({
      url: '/pages/search/seaerch',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})